package cn.cloudplug.core.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
import android.view.Window;

import cn.cloudplug.core.index.Scent;

/**
 * Created by pc on 2018/1/19.
 */

public class MyUtil {

    // dip转像素
    public static int DipToPixels(Context context, int dip) {
        final float SCALE = context.getResources().getDisplayMetrics().density;

        float valueDips = dip;
        int valuePixels = (int) (valueDips * SCALE + 0.5f);

        return valuePixels;

    }

    // 像素转dip
    public static float PixelsToDip(Context context, int Pixels) {
        final float SCALE = context.getResources().getDisplayMetrics().density;
        return Pixels / SCALE;
    }

    public static Bitmap loadBitmapFromViewBySystem(View v) {
        if (v == null) {
            return null;
        }
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        Bitmap bitmap = v.getDrawingCache();
        return bitmap;
    }
    public static void blur(Bitmap overlay, View view, int radius) {
        RenderScript rs = RenderScript.create(Scent.getAppContext());
        Canvas canvas = new Canvas(overlay);
        Paint paint = new Paint();
        paint.setColor(0xccFFFFFF);
        canvas.drawRect(0, 0, overlay.getWidth(), overlay.getHeight(), paint);
        Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
        ScriptIntrinsicBlur blur =
                ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(overlay);
        view.setBackground(new BitmapDrawable(Scent.getAppContext().getResources(), overlay));
        rs.destroy();
    }
/*
    // Remix Blur
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static void blur(Bitmap overlay, View view, int radius) {
        RenderScript rs = RenderScript.create(Scent.getAppContext());

        Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
        ScriptIntrinsicBlur blur =
                ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(overlay);
        view.setBackground(new BitmapDrawable(Scent.getAppContext().getResources(), overlay));
        rs.destroy();
    }

    public static Bitmap blur(Bitmap overlay ,int radius) {
        RenderScript rs = RenderScript.create(Scent.getAppContext());

        Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
        ScriptIntrinsicBlur blur =
                ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(overlay);
        rs.destroy();
        return overlay;
    }

    // Remix Blur
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static void blur(Bitmap overlay, ImageView view, int radius) {
        RenderScript rs = RenderScript.create(Scent.getAppContext());

        Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
        ScriptIntrinsicBlur blur =
                ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(overlay);
        view.setImageBitmap( overlay);
        rs.destroy();
    }
*/
    public static void positionByAngle(Point center,int radius,int angle,float[] coords){
        RectF area = new RectF(center.x - radius, center.y - radius, center.x + radius, center.y + radius);
        Path orbit = new Path();
        orbit.addArc(area, angle,1);
        PathMeasure measure = new PathMeasure(orbit, false);
        measure.getPosTan(0, coords, null);
    }

    private static Point getActionViewCoordinates(Activity activity,View mainActionView,boolean systemOverlay) {
        int[] coords = new int[2];
        // This method returns a x and y values that can be larger than the dimensions of the device screen.
        mainActionView.getLocationOnScreen(coords);

        // So, we need to deduce the offsets.
        if(systemOverlay) {
            coords[1] -= getStatusBarHeight(Scent.getAppContext());
        }
        else {
            Rect activityFrame = new Rect();
            View root=getActivityContentView(activity.getWindow());
            root.getWindowVisibleDisplayFrame(activityFrame);
            coords[0] -= (getScreenSize(activity).x - root.getMeasuredWidth());
            coords[1] -= (activityFrame.height() + activityFrame.top - root.getMeasuredHeight());
        }
        return new Point(coords[0], coords[1]);
    }

    public static Point getActionViewCenter(Activity activity,View mainActionView,boolean systemOverlay) {
        Point point = getActionViewCoordinates(activity,mainActionView,systemOverlay);
        point.x += mainActionView.getMeasuredWidth() / 2;
        point.y += mainActionView.getMeasuredHeight() / 2;
        return point;
    }

    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static View getActivityContentView(Window window) {
        try {
            return window.getDecorView().findViewById(android.R.id.content);
        }
        catch(ClassCastException e) {
            throw new ClassCastException("Please provide an Activity context for this FloatingActionMenu.");
        }
    }

    private static Point getScreenSize(Activity activity) {
        Point size = new Point();
        activity .getWindowManager().getDefaultDisplay().getSize(size);
        return size;
    }
    private final static class measure{
        private static final int W = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        private static final int H = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    }
    public static void viewMeasure(View view){
        view.measure(measure.W,measure.H);
    }
}
